package cur.tool;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

// 生成验证码
public class WatermarkImage {

    public static void main(String[] args) throws IOException {
        String backgroundImagePath = "background.jpg"; // 背景图片路径
        String outputImagePath = "output.png"; // 输出图片路径
        String text = "ABCDE"; // 验证码文本

        BufferedImage background = ImageIO.read(new File(backgroundImagePath));
        Graphics2D g2d = (Graphics2D) background.getGraphics();

        // 设置验证码字体和颜色
        Font font = new Font("Arial", Font.BOLD, 30);
        Color color = Color.BLACK;
        g2d.setFont(font);
        g2d.setColor(color);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // 在背景图片上绘制验证码文本
        int x = 10; // 文本开始的x坐标
        int y = background.getHeight() - 10; // 文本开始的y坐标
        char[] chars = text.toCharArray();
        for (char c : chars) {
            g2d.drawString(String.valueOf(c), x, y);
            x += 30; // 根据字体大小调整间隔
        }

        // 保存图片
        ImageIO.write(background, "png", new File(outputImagePath));
    }
}
